"""
@name : 购买商品
@author : wangshenghu
@projectname: tlbb
@Date  : 2022/6/11
"""
import json
import os
if os.path.exists("vip.txt"):
    with open("vip.txt") as fp:
        # 判断文件是否为空
        if fp.read().strip():
            fp.seek(0)
            # # 把json格式的字符串转化成对应的数据结构对象
            user = json.loads(fp.read())
        else:
            user = {}
else:
    with open("vip.txt", "w", encoding="utf-8") as fp:
        print("已成功初始化vip客户文件。")


class register():
    # 注册函数
    def __init__(self, name, passwd, money):
        self.name = name
        self.passwd = passwd
        self.money = money
        with open("vip.txt", "w", encoding="utf-8") as fp:
            user[self.name] = {"passwd": self.passwd, "money": self.money}
            # #把对应的数据结构对象转化成json格式的字符串 写入文件存储起来
            fp.write(json.dumps(user))
        print("/***注册成功***/")

class login():
    # 登录验证函数
    def __init__(self, name, passwd):
        """
        登录验证函数
        :param name:
        :param passwd:
        :return:
        """
        self.name = name
        self.passwd = passwd
        if self.name in user:
            if self.passwd == user[self.name]["passwd"]:
                print("登录成功".center(50, "#"))
            else:
                print("密码输入错误！")
        else:
            print("用户不存在或用户名输入错误！")


# 商品类
class Goods:
    def __init__(self, classify, product_name, price, store):
        self.classify = classify
        self.product_name = product_name
        self.price = price
        self.store = store
        print("今日商品：".center(50, '*'))
        print(f"商品类别：{self.classify}    商品名称：{self.product_name}    商品价格：{self.price}   商品库存：{self.store}")


class shoping:
    def __init__(self, name, money):
        self.name = name
        self.money = int(money)

    def buy_goods(self, good, weight):
        if weight > good.store:
            print("库存不足！")
        else:
            pay = weight * good.price
            if self.money >= pay:
                self.money -= pay
                good.store -= weight
                user[name]['money'] = self.money
                print(f"购买成功，{name}剩余{self.money}元，"
                      f"{good.product_name}，剩余库存{good.store}斤")

            else:
                print("余额不足！")


while True:
    print("/    欢迎来到三乐购物系统    /".center(50, "*"))
    print("1.注册".center(50, " "))
    print("2.登录".center(50, " "))
    print("3.exit".center(51, " "))
    option = input("请输入你的选择：")
    if option == '1':
        print("/    欢迎注册三乐超市！    /".center(50, "*"))
        name = input("请输入用户名:")
        passwd = input("请输入密码：")
        money = input("请输入您的充值金额：")
        user1 = register(name, passwd, money)
    elif option == '2':
        print("/    欢迎登录三乐购物系统    /".center(50, "="))
        name = input("请输入用户名:")
        passwd = input("请输入密码：")
        if login(name, passwd):
            user1 = shoping(user[name], user[name]['money'])
            print("/===登录成功！===/".center(50, " "))
            apple = Goods("fruits", "apple", 8, 30)
            good_option = input("请输入您想购买的商品：")
            good_num = int(input("请输入您想购买的数量"))
            user1.buy_goods(apple, good_num)
    elif option == '3':
        # 再次写入用户信息，达到更新余额的效果
        with open("vip.txt", "w", encoding="utf-8") as fp:
            fp.write(json.dumps(user))
        print("退出成功！")
        print("欢迎您下次再来！")
        break